<template>
  <router-view />
</template>

<script>
import $ from 'jquery';
export default {
  data() {
    return {
      gapTime: 0,
      beforeUnloadTime: 0
    };
  },
  mounted() {
    window.addEventListener('beforeunload', e => this.beforeunloadFn());
    window.addEventListener('unload', e => this.unloadFn());
  },
  methods: {
    unloadFn() {
      this.gapTime = new Date().getTime() - this.beforeUnloadTime;
      const token = window.sessionStorage.getItem('token');
      if (this.gapTime <= 5) {
        $.ajax({
          type: 'post',
          url: 'http://localhost:9005/user/login/deleteToken',
          beforeSend: function (request) {
            request.setRequestHeader('token', token);
          },
          success: function (result) {
            window.localStorage.removeItem('token');
          },
          async: false
        });
      }
    },
    beforeunloadFn() {
      this.beforeUnloadTime = new Date().getTime();
    }
  },
  destroyed() {
    window.removeEventListener('beforeunload', e => this.beforeunloadFn());
    window.removeEventListener('unload', e => this.unloadFn());
  }
};
</script>

<style>
</style>
